// pages/address/user-address/user-address.js
var app = getApp()
Page({
  data: {
    address: [],
    radioindex: '',
    pro_id: 0,
    num: 0,
    cartId: 0,
    vip:0
  },
  onLoad: function (options) {
    var that = this; console.log(options);
    // 页面初始化 options为页面跳转所带来的参数
    if (options.cartId != undefined && options.cartId != 0) {
      var cartId = options.cartId;
      that.setData({
        cartId: cartId,
      })
    } else if (options.vip != undefined) {
      wx.redirectTo({
        url: '../product/vip2'
      }); wx.redirectTo({
        url: '../product/vip2'
      }); wx.redirectTo({
        url: '../product/vip2'
      });
      var vip = options.vip;
      that.setData({
        vip: vip,
      })
    }
    wx.request({
      url: app.d.hostUrl + 'wapi/address_index.html',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },

      success: function (res) {
        // success
        var address = res.data.data;
        if (address == null) {
          var address = []
        }

        that.setData({
          address: address,
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })

  },

  onShareAppMessage: function () {
    return {
      title: '极好生活商城',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  setDefault: function (e) {
    var that = this;
    var addrId = e.currentTarget.dataset.id;
    wx.request({
      url: app.d.hostUrl + 'wapi/set_default.html',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        addr_id: addrId
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },

      success: function (res) {
        // success
        var status = res.data.status;
        var cartId = that.data.cartId;
        if (status == 0) {
          if (cartId != undefined && cartId != 0) {
            wx.redirectTo({
              url: '../../order/pay?cartId=' + cartId,
            });
          } else {
            var vip = that.data.vip;
            wx.redirectTo({
              url: '../../order/freeget?free=1&back=1&vip=' + vip,
            });
          }

          wx.showToast({
            title: '操作成功！',
            duration: 2000
          });

          that.DataonLoad();
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  delAddress: function (e) {
    var that = this;
    var addrId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '你确认移除吗',
      success: function (res) {
        res.confirm && wx.request({
          url: app.d.hostUrl + 'wapi/consignee_delete.html',
          data: {
            seesionid: app.globalData.userInfo.sessionId,
            openid: app.globalData.userInfo.openid,
            id: addrId
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {// 设置请求的 header
            'Content-Type': 'application/x-www-form-urlencoded'
          },

          success: function (res) {
            // success
            var status = res.data.ret;
            if (status == 0) {
              wx.showToast({
                title: '删除成功。',
                duration: 2000
              });
              that.DataonLoad();
            } else {
              wx.showToast({
                title: res.data.msg,
                duration: 2000
              });
            }
          },
          fail: function () {
            // fail
            wx.showToast({
              title: '网络异常！',
              duration: 2000
            });
          }
        });
      }
    });

  },
  DataonLoad: function () {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    wx.request({
      url: app.d.hostUrl + 'wapi/address_index.html',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },

      success: function (res) {
        // success
        var address = res.data.data;
        if (address == '') {
          var address = []
        }
        that.setData({
          address: address,
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })

  },
})